//
//  MessagePopView.m
//  GameSDK
//
//  Created by Johnny on 2017/7/31.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKMessagePopView.h"
#import "Masonry.h"
#import "GSDKCommonMethod.h"
#import "UIViewController+Extension.h"
#import "YYKit.h"
@interface GSDKMessagePopView()
@property (nonatomic, strong) UIView *messagePopView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIColor *bgColor;
@end

@implementation GSDKMessagePopView
- (BOOL)shouldAutorotate {
    return YES;
}

- (void)showAt:(NSString *)string {
    [self updateConstraintsIfNeeded];
    if (_messagePopView) {
        [_messagePopView removeFromSuperview];
    }
    
    CGSize msgSize = [string sizeWithFont:[UIFont systemFontOfSize:15]
                        constrainedToSize:CGSizeMake([GSDKCommonMethod getWidth] - 10, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    
    //小窗视图
    _messagePopView = [UIView new];
    _messagePopView.backgroundColor = [UIColor colorWithHexString:@"#0197d2"];
    if (_bgColor != nil) {
        _messagePopView.backgroundColor = _bgColor;
    }
    [_messagePopView setUserInteractionEnabled:NO];
    _messagePopView.alpha = 1;
    _messagePopView.layer.cornerRadius = (msgSize.height + 16)/2;
    [[UIViewController getCurrentRootViewController].view addSubview:_messagePopView];
    
    
    
    [_messagePopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-64);
        make.height.mas_equalTo(msgSize.height + 16);
        make.width.mas_equalTo((msgSize.width + 32));
        make.centerX.mas_equalTo(0);
    }];
    
    //小窗标签
    _titleLabel = [[UILabel alloc] init];
    [_titleLabel setTextColor:[UIColor whiteColor]];
    [_titleLabel setBackgroundColor:[UIColor clearColor]];
    [_titleLabel setFont:[UIFont systemFontOfSize:15]];
    [_titleLabel setText:string];
    [_titleLabel setTextAlignment:NSTextAlignmentCenter];
    [_messagePopView addSubview:_titleLabel];
    [_titleLabel setNumberOfLines:2];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(@0);
        make.left.equalTo(@0);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.messagePopView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
        }];
        [UIView animateWithDuration:0.45 animations:^{
            [[UIViewController getCurrentRootViewController].view layoutIfNeeded];
        } completion:^(BOOL finished) {
            [self performSelector:@selector(hideWithWindow:) withObject:self afterDelay:1];
        }];
    });
}

- (void)setColor:(UIColor *)color {
    self.bgColor = color;
}

//小窗隐藏动画
- (void)hideWithWindow:(UIView *)view {
    [self.messagePopView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-64);
    }];
    [UIView animateWithDuration:0.45 animations:^{
        [[UIViewController getCurrentRootViewController].view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.messagePopView removeFromSuperview];
    }];
}

+ (void)showAt:(NSString *)string andBGColor:(UIColor *)bgColor {
    GSDKMessagePopView * popView = [[GSDKMessagePopView alloc] init];
    [popView setColor:bgColor];
    [popView showAt:string];
}
@end
